package com.zhuss.core.login;

import java.util.Arrays;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zhuss.bean.bo.core.auth.AuthBo;
import com.zhuss.bean.bo.core.login.LoginBo;
import com.zhuss.bean.bo.core.login.LogoutBo;
import com.zhuss.bean.bo.core.login.UpdatePicBo;
import com.zhuss.bean.bo.core.login.UpdatePwdBo;
import com.zhuss.bean.bo.core.login.app.UpdateUserBo;
import com.zhuss.bean.bo.core.login.wechat.BindBo;
import com.zhuss.bean.bo.core.login.wechat.InitBo;
import com.zhuss.bean.bo.core.login.wechat.RegisterBo;
import com.zhuss.bean.bo.core.login.wechat.UnbindBo;
import com.zhuss.bean.bo.core.user.CreateUserBo;
import com.zhuss.bean.vo.core.auth.AuthVo;
import com.zhuss.bean.vo.core.login.LoginVo;
import com.zhuss.bean.vo.core.login.app.BindVo;
import com.zhuss.common.AbstractBo;
import com.zhuss.common.LoginOperateService;
import com.zhuss.common.Processor;
import com.zhuss.core.processor.UserRead;
import com.zhuss.exception.DzcxException;

/**
 * 登录service（基本操作）
 * @author zhushanshuo
 * @Date 2019-12-13 15:59
 *
 */
@Component("loginService")
public class LoginService implements LoginOperateService<Object, Object> {
	
	
	@Autowired
	@Qualifier("loginProcessor")
	private Processor<LoginVo, LoginBo> loginProcessor;
	
	@Autowired
	@Qualifier("bindProcessor")
	private Processor<BindVo, com.zhuss.bean.bo.core.login.app.BindBo> bindProcessor;
	
	@Autowired
	@Qualifier("userCreate")
	private Processor<Object, CreateUserBo> userCreate;
	
	@Autowired
	@Qualifier("userUpdate")
	private Processor<Object, com.zhuss.bean.bo.core.user.UpdateUserBo> userUpdate;
	
	@Autowired
	@Qualifier("userRead")
	private UserRead userRead;
	
	@Autowired
	@Qualifier("logoutProcessor")
	private Processor<Object, LogoutBo> logoutProcessor;
	
	@Autowired
	@Qualifier("unbindProcessor")
	private Processor<Object, com.zhuss.bean.bo.core.login.app.UnbindBo> unbindProcessor;
	
	@Autowired
	@Qualifier("pwdUpdateProcessor")
	private Processor<Object, UpdatePwdBo> pwdUpdateProcessor;
	
	@Autowired
	@Qualifier("picUpdateProcessor")
	private Processor<Object, UpdatePicBo> picUpdateProcessor;
	
	@Autowired
	@Qualifier("initProcessor")
	private Processor<LoginVo, InitBo> initProcessor;
	
	@Autowired
	@Qualifier("authTreeProcessor")
	private Processor<AuthVo, AuthBo> authTreeProcessor;
	
	@Autowired
	@Qualifier("authTreeAppProcessor")
	private Processor<AuthVo, AuthBo> authTreeAppProcessor;
	
	@Autowired
	@Qualifier("authMenuProcessor")
	private Processor<AuthVo, AuthBo> authMenuProcessor;
	
	/**
	 * 登录
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Object login(Object param) throws DzcxException {
		
		LoginBo bo = (LoginBo) param;
		Object result = loginProcessor.process(bo);
		return result;
	}
	
	/**
	 * 登出
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Object logout(Object param) throws DzcxException {
		
		LogoutBo bo = (LogoutBo) param;
		logoutProcessor.process(bo);
		return null;
	}
	
	/**
	 * 修改密码
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object updatePwd(Object param) throws DzcxException {
		
		UpdatePwdBo bo = (UpdatePwdBo) param;
		pwdUpdateProcessor.process(bo);
		return null;
	}
	
	/**
	 * 修改头像
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object updatePic(Object param) throws DzcxException {
		
		UpdatePicBo bo = (UpdatePicBo) param;
		picUpdateProcessor.process(bo);
		return null;
	}
	
	/**
	 * 一级菜单
	 */
	public Object firstMenu(Object param) throws DzcxException {
		
		AuthBo bo = (AuthBo) param;
		Object result = authMenuProcessor.process(bo);
		return result;
	}
	
	/**
	 * 子级菜单
	 */
	public Object childMenu(Object param) throws DzcxException {
		
		AuthBo bo = (AuthBo) param;
		Object result = authMenuProcessor.process(bo);
		return result;
	}
	
	/**
	 * 初始化
	 */
	public Object menu(Object param) throws DzcxException {
		
		AuthBo bo = (AuthBo) param;
		Object result = authTreeProcessor.process(bo);
		return result;
	}
	
	/**
	 * 初始化 app 的 menu
	 */
	public Object menuApp(Object param) throws DzcxException {
		
		AuthBo bo = (AuthBo) param;
		Object result = authTreeAppProcessor.process(bo);
		return result;
	}
	
	/**
	 * 初始化
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object init(Object param) throws DzcxException {
		
		InitBo bo = (InitBo) param;
		Object result = initProcessor.process(bo);
		return result;
	}
	
	/**
	 * 注册
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object register(Object param) throws DzcxException {
		
		RegisterBo bo = (RegisterBo) param;
		CreateUserBo createUserBo = new CreateUserBo();
		BeanUtils.copyProperties(bo, createUserBo);
		createUserBo.setName("自行注册");
		createUserBo.setCompanyId(3l);
		createUserBo.setRoleIdList(Arrays.asList(10l));
		userCreate.process(createUserBo);
		return null;
	}
	
	/**
	 * 小程序
	 * 绑定
	 * 登录
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object bind(Object param) throws DzcxException {
		
		BindBo bo = (BindBo) param;
		LoginBo loginBo = new LoginBo();
		BeanUtils.copyProperties(bo, loginBo);
		Object result = loginProcessor.process(loginBo);
		return result;
	}
	
	
	/**
	 * APP
	 * 绑定
	 * 登录
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object bindFromApp(Object param) throws DzcxException {
		
		com.zhuss.bean.bo.core.login.app.BindBo bo = (com.zhuss.bean.bo.core.login.app.BindBo) param;
		Object result = bindProcessor.process(bo);
		return result;
	}
	
	/**
	 * 小程序
	 * 解绑
	 * 登出
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object unbind(Object param) throws DzcxException {
		
		UnbindBo bo = (UnbindBo) param;
		LogoutBo logoutBo = new LogoutBo();
		BeanUtils.copyProperties(bo, logoutBo);
		logoutProcessor.process(logoutBo);
		return null;
	}
	
	/**
	 * App
	 * 解绑
	 * 登出
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object unbindFromApp(Object param) throws DzcxException {
		
		com.zhuss.bean.bo.core.login.app.UnbindBo bo = (com.zhuss.bean.bo.core.login.app.UnbindBo) param;
		unbindProcessor.process(bo);
		return null;
	}
	
	/**
	 * App
	 * 修改用户信息
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object updateUser(Object param) throws DzcxException {
		
		UpdateUserBo bo = (UpdateUserBo) param;
		com.zhuss.bean.bo.core.user.UpdateUserBo updateBo = new com.zhuss.bean.bo.core.user.UpdateUserBo();
		BeanUtils.copyProperties(bo, updateBo);
		updateBo.setUserId(bo.getConcurrentUserId());
		userUpdate.process(updateBo);
		Object vo = userRead.detail(bo.getConcurrentUserId());
		return vo;
	}
	
	/**
	 * 查询用户信息
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object info(Object param) throws DzcxException {
		
		AbstractBo bo = (AbstractBo) param;
		Long userId = bo.getConcurrentUserId();
		Object vo = userRead.detail(userId);
		return vo;
	}
	
}

